{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.8 C\u548cPython\u4e2d\u7684\u7ebf\u7a0b\u6df7\u7528\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u4e2a\u7a0b\u5e8f\u9700\u8981\u6df7\u5408\u4f7f\u7528C\u3001Python\u548c\u7ebf\u7a0b\uff0c\n\u6709\u4e9b\u7ebf\u7a0b\u662f\u5728C\u4e2d\u521b\u5efa\u7684\uff0c\u8d85\u51fa\u4e86Python\u89e3\u91ca\u5668\u7684\u63a7\u5236\u8303\u56f4\u3002\n\u5e76\u4e14\u4e00\u4e9b\u7ebf\u7a0b\u8fd8\u4f7f\u7528\u4e86Python C API\u4e2d\u7684\u51fd\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u5c06C\u3001Python\u548c\u7ebf\u7a0b\u6df7\u5408\u5728\u4e00\u8d77\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u6b63\u786e\u7684\u521d\u59cb\u5316\u548c\u7ba1\u7406Python\u7684\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u3002\n\u8981\u60f3\u8fd9\u6837\u505a\uff0c\u53ef\u4ee5\u5c06\u4e0b\u5217\u4ee3\u7801\u653e\u5230\u4f60\u7684C\u4ee3\u7801\u4e2d\u5e76\u786e\u4fdd\u5b83\u5728\u4efb\u4f55\u7ebf\u7a0b\u88ab\u521b\u5efa\u4e4b\u524d\u88ab\u8c03\u7528\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "#include <Python.h>\n  ...\n  if (!PyEval_ThreadsInitialized()) {\n    PyEval_InitThreads();\n  }\n  ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u4efb\u4f55\u8c03\u7528Python\u5bf9\u8c61\u6216Python C API\u7684C\u4ee3\u7801\uff0c\u786e\u4fdd\u4f60\u9996\u5148\u5df2\u7ecf\u6b63\u786e\u5730\u83b7\u53d6\u548c\u91ca\u653e\u4e86GIL\u3002\n\u8fd9\u53ef\u4ee5\u7528 PyGILState_Ensure() \u548c PyGILState_Release() \u6765\u505a\u5230\uff0c\u5982\u4e0b\u6240\u793a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "...\n/* Make sure we own the GIL */\nPyGILState_STATE state = PyGILState_Ensure();\n\n/* Use functions in the interpreter */\n...\n/* Restore previous GIL state and return */\nPyGILState_Release(state);\n..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6bcf\u6b21\u8c03\u7528 PyGILState_Ensure() \u90fd\u8981\u76f8\u5e94\u7684\u8c03\u7528 PyGILState_Release() ."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u6d89\u53ca\u5230C\u548cPython\u7684\u9ad8\u7ea7\u7a0b\u5e8f\u4e2d\uff0c\u5f88\u591a\u4e8b\u60c5\u4e00\u8d77\u505a\u662f\u5f88\u5e38\u89c1\u7684\u2014\u2014\n\u53ef\u80fd\u662f\u5bf9C\u3001Python\u3001C\u7ebf\u7a0b\u3001Python\u7ebf\u7a0b\u7684\u6df7\u5408\u4f7f\u7528\u3002\n\u53ea\u8981\u4f60\u786e\u4fdd\u89e3\u91ca\u5668\u88ab\u6b63\u786e\u7684\u521d\u59cb\u5316\uff0c\u5e76\u4e14\u6d89\u53ca\u5230\u89e3\u91ca\u5668\u7684C\u4ee3\u7801\u6267\u884c\u4e86\u6b63\u786e\u7684GIL\u7ba1\u7406\uff0c\u5e94\u8be5\u6ca1\u4ec0\u4e48\u95ee\u9898\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u6ce8\u610f\u7684\u662f\u8c03\u7528 PyGILState_Ensure() \u5e76\u4e0d\u4f1a\u7acb\u523b\u62a2\u5360\u6216\u4e2d\u65ad\u89e3\u91ca\u5668\u3002\n\u5982\u679c\u6709\u5176\u4ed6\u4ee3\u7801\u6b63\u5728\u6267\u884c\uff0c\u8fd9\u4e2a\u51fd\u6570\u88ab\u4e2d\u65ad\u77e5\u9053\u90a3\u4e2a\u6267\u884c\u4ee3\u7801\u91ca\u653e\u6389GIL\u3002\n\u5728\u5185\u90e8\uff0c\u89e3\u91ca\u5668\u4f1a\u6267\u884c\u5468\u671f\u6027\u7684\u7ebf\u7a0b\u5207\u6362\uff0c\u56e0\u6b64\u5982\u679c\u5176\u4ed6\u7ebf\u7a0b\u5728\u6267\u884c\uff0c\n\u8c03\u7528\u8005\u6700\u7ec8\u8fd8\u662f\u53ef\u4ee5\u8fd0\u884c\u7684\uff08\u5c3d\u7ba1\u53ef\u80fd\u8981\u5148\u7b49\u4e00\u4f1a\uff09\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}